import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import AboutMe from '@/components/AboutMe'
import Skill from '@/components/Skill'
import Work from '@/components/Work'
import Education from '@/components/Education'
import Login from '@/components/Login'
import UpdateInfo from '@/components/UpdateInfo'
import UpdateAboutMe from '@/components/UpdateAboutMe'
import UpdateEducation from '@/components/UpdateEducation'
import UpdateWork from '@/components/UpdateWork'
import UpdateSkill from '@/components/UpdateSkill'

// 一下2行代码解决重复点击同一路由页面报错问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)


const rt = new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld,
      redirect:'/aboutMe',
      children:[
        {
          path:"work",
          component:Work,
        },
        {
          path:"aboutMe",
          component:AboutMe,
        },
        {
          path:"skill",
          component:Skill,
        },
        {
          path:"education",
          component:Education,
        },
				{
					path: 'updateInfo',
					name: 'updateInfo',
					component: UpdateInfo,
          children:[
            {
              path:"uaboutme",
              component:UpdateAboutMe,
            },
            {
              path:"ueducation",
              component:UpdateEducation,
            },
            {
              path:"uwork",
              component:UpdateWork,
            },
            {
              path:"uskill",
              component:UpdateSkill,
            },
          ]
				}
      ],
    },{
      path: '/login',
      name: 'Login',
      component: Login,
    }
  ]
});

// 路由守卫
rt.beforeEach((to,from,next)=>{
    if(to.path == '/updateInfo' ){
       let sess =  sessionStorage.getItem('token');
       if(sess){
         let sessArr = sess.split('-');
         let now = new Date().getTime();
         if(now < parseInt(sessArr[1])){
            return next();
         }
       }
    }
    next();
})




export default rt

